package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 菜品管理
 */

@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "用户菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishServiceImpl dishServiceImpl;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品和口味信息")
    public Result<List<DishVO>> getByCategoryId(@RequestParam Long categoryId){

        //构造redis中的key，规则：dish_分类id
        String key = "dish_"+categoryId;

        //查询redis中是否存在菜品数据
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if(list != null && list.size() > 0){
            //如果存在，直接返回，无须查询数据库
            return Result.success(list);
        }

        //如果不存在，查询数据库，将查询到的数据放入redis中

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        log.info("根据分类id查询菜品和口味信息: {}",categoryId);
        list = dishServiceImpl.getByCategoryIdWithFlavor(dish);

        redisTemplate.opsForValue().set(key,list);

        return Result.success(list);
    }
}
